package impl

import (
	"gf-xb-api/cwt/filter/model"
	parserRule "gf-xb-api/cwt/filter/parser/rule"

	"github.com/gogf/gf/v2/database/gdb"
)

type OrFilterGroupParser[T any] struct {
	QueryWrapper *gdb.Model
	Rule         *model.FilterRule[T]
	Group        *model.FilterGroup[T]
}

func (d *OrFilterGroupParser[T]) MergeQueryWrappers(b1 *gdb.WhereBuilder, b2 *gdb.WhereBuilder) *gdb.WhereBuilder {
	return b1.WhereOr(b2)
}

func (d *OrFilterGroupParser[T]) ParseWrapperRule(rule *model.FilterRule[T], queryWrapper *gdb.Model) (*gdb.WhereBuilder, error) {

	ruleParser, err := parserRule.GetRuleParser[T](rule.Operate)
	if err != nil {
		return nil, err
	}

	fieldName := parserRule.GetDbFieldName[T](rule.Field)
	builder := ruleParser.Parse(model.OrOperator, fieldName, rule, queryWrapper)
	return builder, nil
}
